/**
 * Copyright (C) 2025 ROKAE (Beijing) Technology Co., LTD.
 * All Rights Reserved.
 *
 * Information in this file is the intellectual property of Rokae Technology Co., Ltd,
 * And may contains trade secrets that must be stored and viewed confidentially.
 *
 * @file InspireRobotsService.cpp
 * @brief 自定义协议
 */
#include "InspireRobotsService.h"

std::string json_serialize(const Json::Value &value)
{
    std::string raw = Json::FastWriter().write(value);
    raw[raw.length() - 1] = '\r'; // ending control, instead of \n
    return raw;
}

void ServiceProtocol()
{
    /**
     * @brief 插件名称，插件的key，回调函数
     * 控制器收到json协议就会回调对应的函数
     * 用户收到的协议数据为{"key":"value"}
     */
    xcore_api::service::RegServiceAPI("InspireRobotsPlugin", "start",
        [](const Json::Value &in) -> Json::Value
        {
            LOG(INFO) << "receive:" << json_serialize(in);
            int slave_id = in["start"]["slave_id"].asInt();
            int angle_set0 = in["start"]["angle_set0"].asInt();
            int angle_set1 = in["start"]["angle_set1"].asInt();
            int angle_set2 = in["start"]["angle_set2"].asInt();
            int angle_set3 = in["start"]["angle_set3"].asInt();
            int angle_set4 = in["start"]["angle_set4"].asInt();
            int angle_set5 = in["start"]["angle_set5"].asInt();

            bool ret = inspire_robots_api.startInspireRobots(slave_id, 
                angle_set0, angle_set1, angle_set2, 
                angle_set3, angle_set4, angle_set5);

            Json::Value out;
            out["return"] = ret;
            LOG(INFO) << "response:" << json_serialize(out);
            return out;
        });

    xcore_api::service::RegServiceAPI("InspireRobotsPlugin", "set_id",
        [](const Json::Value &in) -> Json::Value
        {
            LOG(INFO) << "receive:" << json_serialize(in);
            int slave_id = in["set_id"]["slave_id"].asInt();
            int new_slave_id = in["set_id"]["new_slave_id"].asInt();

            bool ret = inspire_robots_api.setIDInspireRobots(slave_id, new_slave_id);

            Json::Value out;
            out["return"] = ret;
            LOG(INFO) << "response:" << json_serialize(out);
            return out;
        });

    xcore_api::service::RegServiceAPI("InspireRobotsPlugin", "get_angle",
        [](const Json::Value &in) -> Json::Value
        {
            LOG(INFO) << "receive:" << json_serialize(in);
            int slave_id = in["get_angle"]["slave_id"].asInt();
            int angle_set0, angle_set1, angle_set2, angle_set3, angle_set4, angle_set5;

            bool ret = inspire_robots_api.getAngleInspireRobots(slave_id,
                angle_set0, angle_set1, angle_set2,
                angle_set3, angle_set4, angle_set5);

            Json::Value out;
            out["return"] = ret;
            out["angle_set0"] = angle_set0;
            out["angle_set1"] = angle_set1;
            out["angle_set2"] = angle_set2;
            out["angle_set3"] = angle_set3;
            out["angle_set4"] = angle_set4;
            out["angle_set5"] = angle_set5;

            LOG(INFO) << "response:" << json_serialize(out);
            return out;
        });

    xcore_api::service::RegServiceAPI("InspireRobotsPlugin", "set_force",
        [](const Json::Value &in) -> Json::Value
        {
            LOG(INFO) << "receive:" << json_serialize(in);
            int slave_id = in["set_force"]["slave_id"].asInt();
            int force_set0 = in["set_force"]["force_set0"].asInt();
            int force_set1 = in["set_force"]["force_set1"].asInt();
            int force_set2 = in["set_force"]["force_set2"].asInt();
            int force_set3 = in["set_force"]["force_set3"].asInt();
            int force_set4 = in["set_force"]["force_set4"].asInt();
            int force_set5 = in["set_force"]["force_set5"].asInt();

            bool ret = inspire_robots_api.setForceInspireRobots(slave_id,
                force_set0, force_set1, force_set2,
                force_set3, force_set4, force_set5);

            Json::Value out;
            out["return"] = ret;
            LOG(INFO) << "response:" << json_serialize(out);
            return out;
        });

    xcore_api::service::RegServiceAPI("InspireRobotsPlugin", "set_speed",
        [](const Json::Value &in) -> Json::Value
        {
            LOG(INFO) << "receive:" << json_serialize(in);
            int slave_id = in["set_speed"]["slave_id"].asInt();
            int speed_set0 = in["set_speed"]["speed_set0"].asInt();
            int speed_set1 = in["set_speed"]["speed_set1"].asInt();
            int speed_set2 = in["set_speed"]["speed_set2"].asInt();
            int speed_set3 = in["set_speed"]["speed_set3"].asInt();
            int speed_set4 = in["set_speed"]["speed_set4"].asInt();
            int speed_set5 = in["set_speed"]["speed_set5"].asInt();

            bool ret = inspire_robots_api.setSpeedInspireRobots(slave_id,
                speed_set0, speed_set1, speed_set2,
                speed_set3, speed_set4, speed_set5);

            Json::Value out;
            out["return"] = ret;
            LOG(INFO) << "response:" << json_serialize(out);
            return out;
        });
}